package be.felnederlands.client.content.newword;




import java.util.ArrayList;
import java.util.List;

import sun.tools.tree.NewInstanceExpression;

import be.felnederlands.client.content.GuiElementFactory;
import be.felnederlands.client.content.widgets.HPanel;
import be.felnederlands.client.data.Word;
import be.felnederlands.client.data.WordRepository;
import be.felnederlands.client.navigation.NavigationPanel;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

public class NewWordContent extends Composite{

	private FlowPanel fieldset = new FlowPanel();
	private FlowPanel examplePanel = new FlowPanel();
	private TextBox wordTextbox;
	private TextArea definitionTextArea;
	private List<TextArea> exampleTextAreas = new ArrayList<TextArea>();
	
	public NewWordContent(){
		fieldset.setStyleName("row");
		
		createNewWordInput();
		createNewDefinitionInput();
		createExamplesInput();
		addButtons();

		initWidget(fieldset);
	}

	private void createExamplesInput() {
		FlowPanel span1 = GuiElementFactory.createSpan6Offset3FlowPanel();
		span1.add(GuiElementFactory.createH2panelWithLabel("Voorbeelden:"));
		fieldset.add(span1);
		
		FlowPanel span2 = GuiElementFactory.createSpan6Offset3FlowPanel();
		
		TextArea textArea = createTextArea();
		exampleTextAreas.add(textArea);
		examplePanel.add(textArea);
		
		span2.add(examplePanel);
		
		Button newExampleButton = new Button("Nieuw voorbeeld");
		newExampleButton.setStyleName("btn");
		newExampleButton.addStyleName("btn-mini");
		newExampleButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				TextArea newExampleInput = createTextArea();
				examplePanel.add(newExampleInput);
				exampleTextAreas.add(newExampleInput);
			}
		});
		span2.add(newExampleButton);
		fieldset.add(span2);
	}

	private void createNewDefinitionInput() {
		FlowPanel span1 = GuiElementFactory.createSpan6Offset3FlowPanel();
		span1.add(GuiElementFactory.createH2panelWithLabel("Definitie: "));
		fieldset.add(span1);
		
		FlowPanel span2 = GuiElementFactory.createSpan6Offset3FlowPanel();
		definitionTextArea = createTextArea();
		span2.add(definitionTextArea);
		fieldset.add(span2);
		
	}

	private TextArea createTextArea() {
		TextArea textArea = new TextArea();
		textArea.setStyleName("input-span6");
		textArea.getElement().setAttribute("rows", "8");
		return textArea;
	}

	private void createNewWordInput() {
		FlowPanel span1 = GuiElementFactory.createSpan6Offset3FlowPanel();
		
		HPanel h2panel = GuiElementFactory.createH2panelWithLabel("Woord:");
		span1.add(h2panel);
		fieldset.add(span1);
		
		FlowPanel span2 = GuiElementFactory.createSpan6Offset3FlowPanel();
		wordTextbox = new TextBox();
		wordTextbox.setStyleName("input-xlarge");
		span2.add(wordTextbox);
		fieldset.add(span2);
		
	}

	private FlowPanel addButtons() {
		FlowPanel buttons = GuiElementFactory.createSpan6Offset3FlowPanel();
		buttons.addStyleName("button-background");
		
		Button save = new Button("Opslaan");
		save.setStyleName("btn");
		save.addStyleName(" btn-primary");
		
		Button cancel = new Button("Annuleren");
		cancel.setStyleName("btn");
		
		save.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				WordRepository.getInstance().addWord(
						new Word(
								wordTextbox.getText(),
								definitionTextArea.getText(),
								getExamples()));
				
				History.newItem(NavigationPanel.NavigationElement.HOME.name());
			}

			
		});
		
		buttons.add(save);
		buttons.add(cancel);
		fieldset.add(buttons);
		
		return buttons;
	}
	private List<String> getExamples() {
		List<String> result = new ArrayList<String>();
		
		for (TextArea example : exampleTextAreas) {
			result.add(example.getText());
		}
		return result;
	}
	
}
